package com.xk._02算法篇._10skipList;

import com.xk.tools.Asserts;
import com.xk.tools.Times;

import java.util.TreeMap;

/**
 * @description:
 * @author: xu
 * @date: 2022/10/21 19:16
 */
public class Main {
    public static void main(String[] args) {
        /*SkipList<Integer, Integer> list = new SkipList<>();
        testSkipList(list, 30, 10);*/
        time();
    }

    private static void time() {
        SkipList<Integer, Integer> list = new SkipList<>();
        TreeMap<Integer, Integer> map = new TreeMap<>();

        int count = 100_0000;
        int delta = 10;

        Times.test("SkipList", () -> testSkipList(list, count, delta));
        Times.test("TreeMap", () -> testTreeMap(map, count, delta));
    }

    private static void testSkipList(SkipList<Integer, Integer> list, int count, int delta){
        for (int i = 0; i < count; i++) {
            list.put(i, i + delta);
        }
        //System.out.println(list);
        for (int i = 0; i < count; i++) {
            Asserts.test(list.get(i) == i + delta);
        }
        Asserts.test(list.size() == count);
        for (int i = 0; i < count; i++) {
            Asserts.test(list.remove(i) == i + delta);
        }
        Asserts.test(list.size() == 0);
    }

    private static void testTreeMap(TreeMap<Integer, Integer> map, int count, int delta){
        for (int i = 0; i < count; i++) {
            map.put(i, i + delta);
        }
        //System.out.println(list);
        for (int i = 0; i < count; i++) {
            Asserts.test(map.get(i) == i + delta);
        }
        Asserts.test(map.size() == count);
        for (int i = 0; i < count; i++) {
            Asserts.test(map.remove(i) == i + delta);
        }
        Asserts.test(map.size() == 0);
    }
}
